package com.tqyan.service.chat.impl.paper;

import com.tqyan.dal.mapper.PaperMapper;
import com.tqyan.model.dto.*;
import com.tqyan.model.enums.PaperPayStatusEnum;
import com.tqyan.service.chat.AbstractChatPointService;
import com.tqyan.service.chat.ChatPointContent;
import com.tqyan.service.executor.job.PaperJob;
import com.tqyan.service.service.PaperService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 聊天指令: 是
 * <p> <功能详细描述> </p>
 *
 * @author 陈晨
 * @version 1.0
 * @date 2023/5/4
 */
@Service
@Slf4j
public class PaperCompleteChatPointService extends AbstractChatPointService {

    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private PaperService paperService;
    @Autowired
    private PaperViewChatPointService paperViewChatPointService;
    @Autowired
    private PaperJob paperJob;

    @Override
    public String getPoint() {
        return ChatPointContent.PAPER_COMPLETE;
    }

    @Override
    public ChatContentDTO chatPoint(UserDTO user, UserSettingDTO userSetting, ChatDTO chat, String content) {
        // 查询用户未完成的论文
        PaperDTO paper = paperMapper.queryIncomplete(user.getId());
        if (null == paper) {
            return null;
        }
        // 论文已支付, 转查看
        if (PaperPayStatusEnum.PAID.getValue() == paper.getPayStatus()) {
            return paperViewChatPointService.chatPoint(user, userSetting, chat, content);
        }

//        // 论文未支付
//        long account = paper.getPrice();
//        if (paperMapper.queryByPayCount(user.getId()) <= 0) {
//            account = 0;
//        }
//        // 是否余额不足
//        if (accountService.isArrears(user.getId(), account)) {
//            throw new DefaultException("501", "余额不足");
//        }
//        // 扣除金额
//        JSONObject explain = new JSONObject();
//        explain.put("info", "撰写论文");
//        explain.put("paper", paper.getId());
//        explain.put("account", account);
//        boolean result = accountService.consumption(user.getId(), account, explain.toJSONString());
//        log.info("[{}] userId={}, paper={}, explain={}, 论文支付, 扣除金额: {}"
//                , this.getPoint(), user.getId(), paper.getTitle(), explain, result);

        // 记录对话
        String answerContent = String.format(ChatPointContent.ANSWER_1001, paper.getTitle());
        ChatContentDTO question = super.createQuestion(user, userSetting, chat, content, null, answerContent);
        ChatContentDTO answer = super.createAnswer(userSetting, question, answerContent);
        log.info("[{}] userId={}, paper={}, 论文支付, 对话入库", this.getPoint(), user.getId(), paper.getTitle());

        // 修改论文状态（已支付）
        boolean result = paperService.pay(user.getId(), paper.getId());
        log.info("[{}] userId={}, paper={}, 论文支付, 修改支付状态: {}"
                , this.getPoint(), user.getId(), paper.getTitle(), result);

        // 提交撰写
        paperJob.write(paper);
        return answer;
    }

}


